<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="head">
        <div>MIX运营后台</div>
      </div>
      <img class="profile-avatar" src="@/assets/img/user-default.png" alt="" />
      <div class="form-box">
        <el-form-item prop="user_name">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="user_name" v-model="loginForm.user_name" placeholder="请输入账号" name="user_name" type="text" tabindex="1" auto-complete="on" clearable />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-form-item class="form-code" prop="google_code">
          <span class="svg-container">
            <svg-icon icon-class="code-img" />
          </span>
          <el-input ref="google_code" v-model="loginForm.google_code" type="text" placeholder="请输入谷歌验证码(6位数字)" name="google_code" tabindex="3" auto-complete="off" @keyup.enter.native="handleLogin" />
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px" @click.native.prevent="handleLogin">登录</el-button>
      </div>
    </el-form>
    <div id="bubble" class="bubble">
      <canvas id="bubble-canvas" class="bubble-canvas"></canvas>
    </div>
  </div>
</template>

<script>
import * as pageBubble from '@/utils/pageBubble'
import store from '@/store'
let timer = null
export default {
  name: 'Login',
  data () {
    const validateusername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位数字'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        user_name: '',
        password: '',
        google_code: ''
      },
      codeUrl: 'https://demo.buildadmin.com/api/common/captcha?server=1&id=123456',
      loginRules: {
        user_name: [
          { required: true, trigger: 'blur', validator: validateusername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: '/'
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted () {
    localStorage.removeItem('vuex')
    localStorage.removeItem('serverTime')
    sessionStorage.removeItem('tabViews')
    timer = setTimeout(() => {
      pageBubble.init()
    }, 1000)
  },
  beforeUnmount () {
    clearTimeout(timer)
    pageBubble.removeListeners()
  },
  methods: {
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    codeClick () {
      this.codeUrl = 'https://demo.buildadmin.com/api/common/captcha?server=1&id=' + new Date().getTime();
    },
    handleLogin () {
      let that = this;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          that.loading = true
          that.$store
            .dispatch('user/login', {
              ...that.loginForm,
              google_code: parseInt(that.loginForm.google_code)
            }).then( async res => {
              that.loading = false
              // 请求公共的数据【渠道列表 展示渠道 多语言 用户账号 全部商品 道具表 用户信息】
              store.dispatch('commonState/updateChannelData',true)
              store.dispatch('commonState/updateShowChannelData',true)
              store.dispatch('commonState/updateLanguageData',true)
              store.dispatch('commonState/updateAccountData',true)
              store.dispatch('commonState/updateGoodData',true)
              store.dispatch('commonState/updatePropData',true)
              await store.dispatch('user/getInfo')
              that.$router.push({ path: '/' })
            })
            .catch(() => {
              that.loading = false
            })
        } else {
          return false
        }
      })
    },
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #606266;
$cursor: #606266;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 40px;
    width: 91%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 40px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #ddd;
    border-radius: 4px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #606266;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background-image: url('~@/assets/img/bg.jpg');
  background-repeat: repeat;
  overflow: hidden;

  .head {
    position: relative;
    width: 430px;
    height: 151px;
    background: #ccccff;
    background: url('~@/assets/img/login-header.png');
    background-size: 430px 151px;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    > div {
      font-size: 30px;
      color: #000;
    }
    // img{
    //   display: block;
    //   width: 430px;
    //   margin: 0 auto;
    //   user-select: none;
    // }
  }
  .profile-avatar {
    display: block;
    position: absolute;
    height: 100px;
    width: 100px;
    border-radius: 50%;
    border: 4px solid #fff;
    top: 100px;
    left: 50%;
    z-index: 2;
    transform: translateX(-50%);
  }

  .form-box {
    padding: 80px 40px 10px;
  }

  .login-form {
    width: 430px;
    max-width: 100%;
    overflow: hidden;
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 200;
    transform: translate(-50%, -50%);
    background-color: #fff;
  }

  .tips {
    font-size: 14px;
    color: $light_gray;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 0 5px 0 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .form-code {
    svg {
      font-size: 17px;
      flex-shrink: 0;
    }
    ::v-deep .el-form-item__content {
      display: flex;
      align-items: center;
    }
    .code-img {
      width: 100px;
      height: 40px;
      flex-shrink: 0;
      object-fit: contain;
      margin-right: 10px;
    }
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .bubble {
    overflow: hidden;
  }
  ::v-deep {
    input:-webkit-autofill {
      transition: background-color 5000s ease-in-out 0s;
      -webkit-box-shadow: 0 0 0px 1000px white inset !important;
    }
  }
}

@media (max-width: 768px) {
  .login-container .login-form {
    width: 90%;
    .head img {
      width: 100%;
      height: 90px;
    }
    .profile-avatar {
      width: 80px;
      height: 80px;
      top: 50px;
    }
    .form-box {
      padding: 70px 20px 0;
    }
    .el-form-item {
      width: 100%;
      ::v-deep {
        .el-form-item__content {
          width: inherit;
          display: flex;
          align-items: center;
        }
      }
    }
  }
}

@media (max-width: 468px) {
  .login-container .login-form {
    .head {
      width: 100%;
      div {
        text-align: center;
      }
    }
    .head + img {
      display: none;
    }
  }
}
</style>